//Brian Goodacre
package data;

import data.DataPoint;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class TextTokenizer {
	private FileInputStream fstream;
	private DataInputStream in;
	private BufferedReader br;
	private boolean open;
	
	//create a TextFileTokenizer
	public TextTokenizer(String file){
		//initialize values that will be reading in the file
		try {
			fstream = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		in = new DataInputStream(fstream);
		br = new BufferedReader(new InputStreamReader(in));
		open = true; //can still read
	}
	
	//returns DataPoint or null if empty
	public DataPoint getNextPoint(){
		if(open==false){//cannot read anymore
			return null; 
		}
		else{
			try{
				String strLine = br.readLine();//read in line
				if(strLine==null){//finish
					in.close();//Close the input stream
					return null;
				}
				else{//convert to DataPoint and return
					StringTokenizer st = new StringTokenizer (strLine, ",");//tokenize this line 
					int x=Integer.parseInt(st.nextToken()); //x value
					int y=Integer.parseInt(st.nextToken()); //y value
					DataPoint dp = new DataPoint(x,y); //create DataPoint
					return dp;
				}
			}
			catch (Exception e){
				System.err.println("Error: " + e.getMessage());
				return null; //error
			}
		}
	}
}
